package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyCustomImg;
import cn.wolfcode.trip.base.domain.StrategyLabel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyCustomImgMapper;
import cn.wolfcode.trip.base.mapper.StrategyLabelMapper;
import cn.wolfcode.trip.base.query.LabelQueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private StrategyCustomImgMapper strategyCustomImgMapper;
    @Autowired
    private StrategyLabelMapper strategyLabelMapper;

    @Override
    public List<StrategyComment> listByComment() {
        return strategyCommentMapper.selectAll();
    }

    @Override
    public List<StrategyComment> getComment(Long strategyId) {
        return strategyCommentMapper.selectByPrimaryKey(strategyId);
    }

    @Override
    public void save(StrategyComment strategyComment, String[] labels, String[] images) {
        User currentUser = UserContext.getCurrentUser();
        if(currentUser!=null){
            strategyComment.setUser(currentUser);
        }
        strategyComment.setReleaseTime(new Date());
        strategyCommentMapper.insert(strategyComment);
        Long id = strategyComment.getId();
        List<StrategyCustomImg> imageList = new ArrayList<>();
        for (String image : images) {
            StrategyCustomImg strategyCustomImg = new StrategyCustomImg();
            strategyCustomImg.setCommentImgId(id);
            strategyCustomImg.setCustomImgUrl(image);
            strategyCustomImgMapper.insert(strategyCustomImg);
        }
        for (String label : labels) {
            if(label!=""){
                StrategyLabel strategyLabel = new StrategyLabel();
                strategyLabel.setCommentLabId(id);
                strategyLabel.setLabelContent(label);
                strategyLabelMapper.insert(strategyLabel);
            }
        }
    }

    @Override
    public PageInfo<StrategyComment> selectForList(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public StrategyComment getCommentMessage(Long id) {
        return strategyCommentMapper.selectByComments(id);
    }

    @Override
    public List<StrategyComment> selectByLabels(Long strategyId) {
        return strategyCommentMapper.selectByLabels(strategyId);
    }

    @Override
    public PageInfo getLabelsByStrategyId(LabelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyLabel> list = strategyCommentMapper.selectLabelsByStrategyId(qo);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo selectForUser(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectForUser(qo);
        return new PageInfo<>(list);
    }

    public PageInfo selectForHeadlines(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyComment> list = strategyCommentMapper.selectForHeadlines(qo);
        return new PageInfo<>(list);
    }
}
